package com.claire.controller;

import com.claire.bean.Student;
import com.claire.bean.Team;
import com.claire.common.ApiResponse;
import com.claire.common.BaseController;
import com.claire.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 社团管理
 *
 * @author tjx
 */
@RestController
@RequestMapping("/team")
public class TeamController extends BaseController {
    @Autowired
    private TeamService teamService;

    //获取社团分页列表：可选条件：社团名称
    @RequestMapping(value="/getAllTeam",method = RequestMethod.GET)
    public ApiResponse getAllTeam(@RequestParam Long pageCurrent,@RequestParam Long pageSize,
                                  @RequestParam(value = "teamname", required = false, defaultValue = "") String teamname){
        return success(teamService.getTeams(pageCurrent,pageSize,teamname));
    }

    //根据id查找社团
    @RequestMapping(value="/getTeamById",method = RequestMethod.GET)
    public ApiResponse getTeamById(Integer id){
        return success(teamService.getTeam(id));
    }

    /**
     * 根据社团类型查找社团
     * @param types
     * @return
     */
    @RequestMapping(value="/getTeamsByType",method = RequestMethod.GET)
    public ApiResponse getTeamsByType(@RequestParam String types){
        return success(teamService.getTeamsByType(types));
    }

    /**
     * 管理页  按条件分页返回社团
     * @param current
     * @param size
     * @param team
     * @return
     */
    @RequestMapping(value = "/getManageTeam",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public ApiResponse getManageTeam(Long current,Long size,@RequestBody Team team){
        return success(teamService.getAllTeam(current,size,team));
    }

    /**
     * 更新社团信息：编辑
     * @param team
     * @return
     */
    @RequestMapping("/updateTeamInfo")
    public ApiResponse updateTeamInfo(@RequestBody Team team){
        return success(teamService.updateTeamInfo(team));
    }

    /**
     * 判断当前登录用户 是否是该社团的管理员
     * @param teamId
     * @param request
     * @return
     */
    @RequestMapping("/isTeamManager")
    public ApiResponse isTeamManager(@RequestParam Integer teamId, HttpServletRequest request){
        Student student=getLoginStudent(request);//获取当前用户
        return success(teamService.isThisManager(student.getId(),teamId));
    }
}
